<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="mini" :inline="true" class="demo-form-inline">
        <el-form-item label="分类名称">
          <el-input v-model="params.catename" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="$refs.addoredit.getdetail('add')">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini" @click="exportExcel">导出</el-button>
        </div>
        <AddorEdit ref="addoredit" @reload="reload" />
        <div>
          <el-tooltip class="item" effect="dark" content="关闭搜索区" placement="top">
            <el-button :icon="up" size="small" circle @click="hide" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <el-button icon="el-icon-refresh" size="small" circle @click="reload" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="筛选" placement="top-start">
            <el-popover placement="bottom" width="100" trigger="click">
              <el-checkbox-group v-model="checkList">

                <el-checkbox class="fa fa fa-dedent" label="分类ID" />

                <el-checkbox class="fa fa fa-dedent" label="分类名称" />
                <el-checkbox class="fa fa fa-dedent" label="图标class" />
                <el-checkbox class="fa fa fa-dedent" label="权重" />
              </el-checkbox-group>
              <el-button slot="reference" class="filter" icon="el-icon-menu" size="small" circle />
            </el-popover>
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table id="outTable" ref="myTable" :data="catelist" style="width: 100%" border>
        <el-table-column type="index" label="#" width="50" />
        <el-table-column v-if="checkList.includes('分类ID')" prop="id" label="分类ID" />
        <el-table-column v-if="checkList.includes('分类名称')" prop="catename" label="分类名称" />
        <el-table-column v-if="checkList.includes('图标class')" prop="" label="图标class">
          <template #default="{row}">
            {{ row.icon }}
            <i class="fa" :class=" row.icon" />
          </template>
        </el-table-column>
        <el-table-column v-if="checkList.includes('权重')" prop="sort_num" label="权重" />
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button size="mini" circle type="primary" icon="el-icon-edit" @click="$refs.addoredit.getdetail(row.id)" />
            <el-button v-power="'ydd_cate@del'" size="mini" type="danger" icon="el-icon-delete" circle @click="dodelete(row.id)" />
          </template>
        </el-table-column>

      </el-table>
    </div>
    <!-- 分页区域 -->
    <div class="page">
      <el-pagination
        v-if="isTableAlive"
        background
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>

import { getsearch, delcate } from '@/api/cate'
import { exportTableAsXLSX } from '@/utils/unitools'
import AddorEdit from '@/components/AddorEdit'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      checkList: ['分类ID', '图标class', '分类名称', '权重'],
      catelist: [],
      params: {
        catename: '',
        page: 1,
        limit: 10
      },
      flag: true,
      total: 0,
      up: 'el-icon-arrow-up',
      isTableAlive: true

    }
  },

  created() {
    this.search()
  },
  methods: {

    dodelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delcate(id)
        console.log(res)
        if (res === '') {
          this.$message.success('删除成功')
          this.reload()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    exportExcel() {
      exportTableAsXLSX('outTable')
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.params.page = 1
      this.params.limit = val
      this.search()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.search()
    },
    reset() {
      this.params.catename = ''
      this.search()
    },

    async search() {
      const res = await getsearch(this.params)
      // console.log(res)
      this.catelist = res.data
      this.total = res.count
    },
    hide() {
      this.flag = !this.flag
      this.up = this.flag ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
    },
    reload() {
      this.catelist = []
      this.params.page = 1
      this.isTableAlive = false
      setTimeout(() => {
        this.isTableAlive = true
      }, 100)
      this.search()
    }

  }
}
</script>
<style lang="scss" scoped>
.page-box {
  padding: 20px;
  .search-box,
  .tabbar-box {
    padding: 10px 10px;
    height: 50px;
    .filter {
      margin-left: 10px;
    }
  }
  .fontsome {
    margin-left: 15px;
    &:hover {
      border-bottom: 1px solid #51a7ff;
      padding-bottom: 5px;
      color: #51a7ff;
    }
  }
}
</style>
